import { ILv70DramaStepCommand } from "../interfaces/ILv70DramaStepCommand";
import { TDrama } from "../../../../../Message/Db/Tables/TDrama";
import { Global } from "../../../../../com/gengine/global/Global";
import { TweenMax } from "../../../../../com/greensock/TweenMax";
import { Lv70NpcTalkData } from "../data/Lv70NpcTalkData";
import { LoaderManager } from "../../../../../com/gengine/resource/LoaderManager";
import { LoaderPriority } from "../../../../../com/gengine/resource/LoaderPriority";
import { Log } from "../../../../../com/gengine/debug/Log";
import { SWFInfo } from "../../../../../com/gengine/resource/info/SWFInfo";
import { Cache } from "../../../cache/Cache";
import { ECamp } from "../../../../../Message/Public/ECamp";
import { ESex } from "../../../../../Message/Public/ESex";
import { LayerManager } from "../../../manager/LayerManager";
import { ObjCreate } from "../../common/ObjCreate";
import { GlobalStyle } from "../../../../common/global/GlobalStyle";
import { Language } from "../../../../../extend/language/Language";
import { Lv70NpcTalkMaskText } from "./npctalk/Lv70NpcTalkMaskText";
import { SWFPlayer } from "../../../scene/modle/SWFPlayer";
import { ObjectPool } from "../../../../../com/gengine/utils/pools/ObjectPool";
import { ModelType } from "../../../scene/modle/data/ModelType";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { GTextFiled } from "../../../../../com/mui/controls/GTextFiled";
type int = number;
//class Lv70OpNpcTalk
    
    export  class Lv70OpNpcTalk /* flash.display.Sprite */ implements ILv70DramaStepCommand
    {
        constructor()
        {
            
            this._isShowing = false;
            this._isKepping = false;
            this.initView();
            return;
        }

        public set isKepping(arg1: Boolean)
        {
            this._isKepping = arg1;
            return;
        }

        public call(arg1: TDrama, arg2: Function=null): void
        {
            if (arg1 == null) 
            {
                return;
            }
            this.iconMouse.visible = false;
            this._config = arg1;
            this._onEnd = arg2;
            if (this._isShowing) 
            {
                this.stopCall();
            }
            this._isShowing = true;
            this._data = this.getNpcTalkData(arg1);
            this.updateView();
            var loc1=this.getHeadIconResFile(arg1);
            if (this._lastIconName == loc1) 
            {
                return;
            }
            this.loadHeadIcon(loc1);
            return;
        }

        public cancel(arg1: TDrama, arg2: Function=null): void
        {
            if (arg2 != null) 
            {
                arg2.apply();
            }
            return;
        }

        public stopCall(): void
        {
            this._text.stopShow();
            this._isShowing = false;
            return;
        }

        /* internal  */resetIconPlaces(): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=0;
            if (this._leftIcon.bitmapData != null) 
            {
                loc2 = (this.AvatarOffPixes - this._leftIcon.width) / 2;
                this._leftIcon.x = -this._leftIcon.width;
                this._leftIcon.y = this.BgHeight - this._leftIcon.height;
                if (this._config.speedUp == 1) 
                {
                    this._leftIcon.y = this._leftIcon.y - 30;
                }
                loc1 = this._leftIcon;
                this.iconMouse.move(Global.stage.stageWidth - this.AvatarOffPixes / 2 + 80, this._text.y + 120);
            }
            if (this._rightIcon.bitmapData != null) 
            {
                loc3 = Global.stage.stageWidth - this.AvatarOffPixes;
                loc2 = loc3 + (this.AvatarOffPixes - this._rightIcon.width) / 2;
                this._rightIcon.y = this.BgHeight - this._rightIcon.height;
                if (this._config.speedUp == 1) 
                {
                    this._rightIcon.y = this._rightIcon.y - 30;
                }
                this._rightIcon.x = Global.stage.stageWidth;
                loc1 = this._rightIcon;
                this.iconMouse.move(this.AvatarOffPixes / 2 + 80, this._text.y + 120);
            }
            if (loc1 == null) 
            {
                this._txtSkip.x = this.AvatarOffPixes / 2 - this._txtSkip.width / 2;
                this._txtSkip.y = Global.stage.stageHeight - 25;
            }
            else 
            {
                TweenMax.to(loc1, 0.4, {"x": loc2});
                this._txtSkip.x = loc2 + loc1.width / 2 - this._txtSkip.width / 2;
                this._txtSkip.y = loc1.y + loc1.height + 6;
            }
            this._text.x = 350;
            this._txtName.x = 320;
            return;
        }

        /* internal  */getNpcTalkData(arg1: TDrama): Lv70NpcTalkData
        {
            var loc1=new Lv70NpcTalkData();
            loc1.popupTime = 0;
            loc1.rowWidth = Global.stage.stageWidth - 700;
            loc1.speed = 4;
            loc1.talk = arg1.text;
            loc1.showTime = 4000;
            loc1.talkFontLeading = 8;
            loc1.talkFontSize = 16;
            return loc1;
        }

        public removeFromStage(): void
        {
            this._isShowing = false;
            if (this.parent != null) 
            {
                this.parent.removeChild(this);
            }
            return;
        }

        /* internal  */onShowEnd(): void
        {
            if (this._onEnd != null) 
            {
                this._onEnd.apply();
                this._onEnd = null;
            }
            if (!(this.parent == null) && !this._isKepping) 
            {
                this.parent.removeChild(this);
            }
            this._isShowing = false;
            return;
        }

        /* internal  */loadHeadIcon(arg1: string): void
        {
            this._lastIconName = arg1;
            this._isLoading = true;
            this._loadingFileName = arg1;
            LoaderManager.instance.load(arg1, this.onNpcHeadLoaded, LoaderPriority.LevelB, this._config.avatarId, null, this.onFail);
            return;
        }

        /* internal  */onFail(arg1=null): void
        {
            Log.debug("failed load resource, named: " + this._loadingFileName);
            return;
        }

        /* internal  */onNpcHeadLoaded(arg1: SWFInfo): void
        {
            this._isLoading = false;
            if (!arg1) 
            {
                return;
            }
            if (!arg1.bitmapData) 
            {
                return;
            }
            if (int(arg1.extData) != this._config.avatarId) 
            {
                return;
            }
            if (this._config.avatrPos != this.AvatarPosLeft) 
            {
                this._rightIcon.bitmapData = arg1.bitmapData;
                this._leftIcon.bitmapData = null;
            }
            else 
            {
                this._leftIcon.bitmapData = arg1.bitmapData;
                this._rightIcon.bitmapData = null;
            }
            this.resetIconPlaces();
            return;
        }

        /* internal  */getHeadIconResFile(arg1: TDrama): string
        {
            var loc1=arg1.avatarId;
            if (this.isPlayerSelf(arg1)) 
            {
                if (Cache.instance.role.playerInfo.camp != ECamp._ECampOne) 
                {
                    if (Cache.instance.role.playerInfo.camp != ECamp._ECampTwo) 
                    {
                        if (Cache.instance.role.playerInfo.camp == ECamp._ECampThree) 
                        {
                            loc1 = 1000004;
                        }
                    }
                    else 
                    {
                        loc1 = 1000002;
                    }
                }
                else 
                {
                    loc1 = 1000000;
                }
                if (Cache.instance.role.playerInfo.sex == ESex._ESexWoman) 
                {
                    loc1 = loc1 + 1;
                }
            }
            return "drama_" + loc1.toString() + ".swf";
        }

        /* internal  */updateView(arg1: Boolean=true): void
        {
            this.iconMouse.visible = true;
            if (this.isPlayerSelf(this._config)) 
            {
                this._txtName.htmlText = Cache.instance.role.playerInfo.name + ":";
            }
            else 
            {
                this._txtName.htmlText = this._config.talkerName + ":";
            }
            this._text.show(this._data, this.onShowEnd);
            this.y = Global.stage.stageHeight - this.BgHeight;
            if (this.parent == null) 
            {
                LayerManager.highestLayer.addChild(this);
            }
            this._txtSkip.y = Global.stage.stageHeight - 25;
            if (this._config.speedUp != 1) 
            {
                this._txtSkip.visible = false;
            }
            else 
            {
                this._txtSkip.visible = true;
            }
            return;
        }

        /* internal  */isPlayerSelf(arg1: TDrama): Boolean
        {
            return arg1.talkerName == "0";
        }

        /* internal  */initView(): void
        {
            this._txtName = ObjCreate.createGTextField("", 500, this.BgHeight / 2 - 33, 100, 20, this);
            this._txtName.textColor = 65066;
            var loc1=this._txtName.defaultTextFormat;
            loc1.size = 16;
            this._txtName.defaultTextFormat = loc1;
            this._txtSkip = ObjCreate.createGTextField("", 0, 0, 100, 20, this);
            this._txtSkip.textColor = GlobalStyle.color1Uint;
            loc1 = this._txtSkip.defaultTextFormat;
            loc1.size = 16;
            loc1.underline = true;
            loc1.align = flash.text.TextFormatAlign.CENTER;
            this._txtSkip.defaultTextFormat = loc1;
            this._txtSkip.htmlText = Language.getString(80121);
            this._txtSkip.addEventListener(flash.events.MouseEvent.CLICK, this.clickSkipHandler);
            this._text = new Lv70NpcTalkMaskText();
            this._text.x = 320;
            this._text.y = this.BgHeight / 2 - 10;
            this.addChild(this._text);
            this._leftIcon = new Object /* flash.display.Bitmap */();
            this._rightIcon = new Object /* flash.display.Bitmap */();
            this.addChild(this._leftIcon);
            this.addChild(this._rightIcon);
            this.addChild(this._txtSkip);
            return;
        }

        /* internal  */get iconMouse(): SWFPlayer
        {
            if (this._iconMouse == null) 
            {
                this._iconMouse = ObjectPool.getObject(SWFPlayer);
                this._iconMouse.timeRate = 3;
                this._iconMouse.load("Lv70DramaMouseMc.swf", ModelType.Effect, null);
                this.addChild(this._iconMouse);
            }
            return this._iconMouse;
        }

        /* internal  */clickSkipHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.Lv70DramaSkipDrama, this._config));
            return;
        }

        public dispose(): void
        {
            if (this._iconMouse != null) 
            {
                this._iconMouse.dispose();
                this._iconMouse = null;
            }
            this._onEnd = null;
            return;
        }

        public get isKepping(): Boolean
        {
            return this._isKepping;
        }

        public doResizeByStage(): void
        {
            if (this._isLoading) 
            {
                return;
            }
            this.resetIconPlaces();
            this._text.resize(Global.stage.stageWidth - 700);
            this.y = Global.stage.stageHeight - this.BgHeight;
            return;
        }

        /* internal */ /* const */BgHeight: int=110;

        /* internal */ /* const */AvatarPosLeft: int=1;

        /* internal */ /* const */AvatarPosRight: int=2;

        /* internal */ /* const */AvatarOffPixes: int=300;

        /* internal */ /* var */_data: Lv70NpcTalkData;

        /* internal */ /* var */_config: TDrama;

        /* internal */ /* var */_text: Lv70NpcTalkMaskText;

        /* internal */ /* var */_txtName: GTextFiled;

        /* internal */ /* var */_txtSkip: GTextFiled;

        /* internal */ /* var */_leftIcon: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_rightIcon: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_onEnd: Function;

        /* internal */ /* var */_isShowing: Boolean;

        /* internal */ /* var */_lastIconName: string;

        /* internal */ /* var */_isKepping: Boolean;

        /* internal */ /* var */_iconMouse: SWFPlayer;

        /* internal */ /* var */_isLoading: Boolean=false;

        /* internal */ /* var */_loadingFileName: string;
    }
